package com.centit.framework.ip.allmodule.controller;

import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseData;
import com.centit.framework.ip.allmodule.po.Models;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.centit.framework.ip.allmodule.service.ModelsManager;
	

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray;
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.core.controller.BaseController;
import com.centit.support.database.utils.PageDesc;
/**
 * Models  Controller.
 * create by scaffold 2018-07-08 
 * @author codefan@sina.com
 * 功能信息表功能信息表   
*/


@Controller
@RequestMapping("/sxfgyth/models")
public class ModelsController  extends BaseController {
	private static final Log log = LogFactory.getLog(ModelsController.class);
	
	@Resource
	private ModelsManager modelsMag;	
	/*public void setModelsMag(ModelsManager basemgr)
	{
		modelsMag = basemgr;
		//this.setBaseEntityManager(modelsMag);
	}*/

    /**
     * 查询所有   功能信息表  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field, PageDesc pageDesc, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> searchColumn = convertSearchColumn(request);        
        
        List jsonArrayObjects = modelsMag.listObjectsAsJson( searchColumn, pageDesc);
         
        if (null == pageDesc) {
            JsonResultUtils.writeSingleDataJson(jsonArrayObjects, response);
            return;
        }

        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);

        JsonResultUtils.writeResponseDataAsJson(resData, response);
    }
    
    /**
     * 查询单个  功能信息表 
	
	 * @param recId  rec_id
     *  
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.GET})
    public void getModels(@PathVariable String recId, HttpServletResponse response) {
    	
    	Models models =     			
    			modelsMag.getObjectById( recId);
        
        JsonResultUtils.writeSingleDataJson(models, response);
    }
    
    /**
     * 新增 功能信息表
     *
     * @param models  {@link Models}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public void createModels(@Valid Models models, HttpServletResponse response) {
    	modelsMag.saveNewObject(models);
        JsonResultUtils.writeSingleDataJson("操作成功",response);
    }

    /**
     * 删除单个  功能信息表 
	
	 * @param recId  rec_id
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.DELETE})
    public void deleteModels(@PathVariable String recId, HttpServletResponse response) {
    	
    	modelsMag.deleteObjectById( recId);
        
        JsonResultUtils.writeBlankJson(response);
    } 
    
    /**
     * 新增或保存 功能信息表 
    
	 * @param recId  rec_id
	 * @param models  {@link Models}
     * @param response    {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.PUT})
    public void updateModels(@PathVariable String recId, 
    	@Valid Models models, HttpServletResponse response) {
    	
    	
    	Models dbModels  =     			
    			modelsMag.getObjectById( recId);
        
        

        if (null != models) {
        	dbModels .copy(models);
        	modelsMag.mergeObject(dbModels);
        } else {
            JsonResultUtils.writeErrorMessageJson("当前对象不存在", response);
            return;
        }

        JsonResultUtils.writeBlankJson(response);
    }
}
